package my.test;

import java.text.SimpleDateFormat;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

public class AutoTask {
    Set<String> set = new HashSet<String>();

    public static SimpleDateFormat asd = new SimpleDateFormat( "yyyy-MM-dd");
    public  boolean addSet(String k) {
        return set.add(k);
    }

    public void test() {
        ExecutorService executorService = createThreadPool(50, "AutoGatherOrderTask", true);
        for (int i = 0; i < 1000; i++) {
            AutoGatherThread gatherThread = new AutoGatherThread(this,asd);
            executorService.execute(gatherThread);
        }
        executorService.shutdown();
    }

    public static final ExecutorService createThreadPool(int size, final String threadName, final boolean daemon) {
        ExecutorService es = Executors.newFixedThreadPool(size, new ThreadFactory() {
            int i = 0;
            @Override
            public Thread newThread(Runnable r) {
                Thread t;
                if (null == threadName || "".equals(threadName.trim())) {
                    t = new Thread(r, "ThreadPool-" + (i++));
                } else {
                    t = new Thread(r, threadName + "-" + (i++));
                }
                if (daemon) {
                    t.setDaemon(daemon);
                }
                return t;
            }
        });
        return es;
    }
}
